#!/bin/bash

set -e
tools_path=/mnt/ilustre/app/medical/tools
data_path=/mnt/ilustre/app/medical/.data

log=.log
if [ ! -e "$log" ]; then
	:> $log
fi

while getopts  "s:p:" opts
do
        case  $opts  in
        s)
		barcode=$OPTARG
		sample_name=$OPTARG
		echo Sample name is: $barcode 2>>$log 1>&2
		;;
		p)
		out_prefix=$OPTARG
		echo out prefix name is: $out_prefix 2>>$log 1>&2
		;;
        esac
done
shift $(($OPTIND - 1))

if [ -z "$2" ]; then
	echo $0 '[-p out_prefix] reads1.fq[.gz], reads2.fq[.gz]'
	exit 1
fi

if [ -z $out_prefix ]; then
	out_prefix=1
fi

# echo $0 '<-s barcode> 1.fq 2.fq'


cutadapt=${tools_path}/cutadapt-1.8.1/bin/cutadapt

truseq_uni=AATGATACGGCGACCACCGAGATCTACACTCTTTCCCTACACGACGCTCTTCCGATCT

truseq_uni_res_com=AGATCGGAAGAGCGTCGTGTAGGGAAAGAGTGTAGATCTCGGTGGTCGCCGTATCATT

truseq_uni_res_com_22=AGATCGGAAGAGCGTCGTGTAG
truseq_uni_res_com_20=AGATCGGAAGAGCGTCGTGT

truseq_0=GATCGGAAGAGCACACGTCTGAACTCCAGTCAC
truseq_1=ATCTCGTATGCCGTCTTCTGCTTG

truseq1=${truseq_0}${barcode}${truseq_1}

truseq_0_22=GATCGGAAGAGCACACGTCTGA
# truseq_0_20=GATCGGAAGAGCACACGTCT
truseq_0_20=AGATCGGAAGAGCACACGTC

tem_seq1=TGGAATTCTCGGGTGCCAAG

tem_seq2=AGATCGGAAGAGCGTCGT

primer5=$data_path/ref/b37/lunca50/primer5N.fa
primer3=$data_path/ref/b37/lunca50/primer3N.fa


# s1=`basename $1 | cut -d . -f 1`
# s2=`basename $2 | cut -d . -f 1`

# if [ -n "$3" ]; then
	# s1=$3
# fi

# if [ -n "$4" ]; then
	# s1=$4
# fi



# $cutadapt \
# -n 2 \
# -q 10 \
# --trimmed-only \
# -m 65 \
# -M 200 \
# -a $truseq_0_20 \
# -A $truseq_uni_res_com_20 \
# -o $s1.cut.fastq \
# -p $s2.cut.fastq \
# $1 \
# $2 \
# 2>>$log 1>&2

# $cutadapt \
# -n 2 \
# -q 10 \
# -m 65 \
# -M 200 \
# -a $truseq_0_20 \
# -A $truseq_uni_res_com_20 \
# -o $s1.cut.fastq \
# -p $s2.cut.fastq \
# $1 \
# $2 \
# 2>>$log 1>&2

:<<!

$cutadapt \
-e 0.13 \
-n 2 \
-m 66 \
--discard-untrimmed \
-a $tem_seq1 \
-A $tem_seq2 \
-o $out_prefix.1.cut.fastq \
-p $out_prefix.2.cut.fastq \
$1 \
$2 \
2>>$log 1>&2

!

$cutadapt \
-e 0.13 \
--overlap 4 \
--discard-untrimmed \
-a file:$primer3 \
-A file:$primer3 \
-g file:$primer5 \
-G file:$primer5 \
-o $out_prefix.1.trimmed.fastq \
-p $out_prefix.2.trimmed.fastq \
$out_prefix.1.cut.fastq \
$out_prefix.2.cut.fastq \
2>>$log 1>&2

# -M 200 \

# $cutadapt \
# -u +20 \
# -u -20 \
# -U +20 \
# -U -20 \
# -m 20 \
# -o $s1.trimmed.fastq \
# -p $s2.trimmed.fastq \
# $s1.cut.fastq \
# $s2.cut.fastq \
# 2>>$log 1>&2

len5=21
len3=25

# $cutadapt \
# -u +$len5 \
# -u -$len3 \
# -U +$len5 \
# -U -$len3 \
# -o $out_prefix.1.trimmed.fastq \
# -p $out_prefix.2.trimmed.fastq \
# $out_prefix.1.cut.fastq \
# $out_prefix.2.cut.fastq \
# 2>>$log 1>&2

